package com.eternal.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eternal.framework.utils.ConvertUtils;
import com.eternal.framework.utils.ScreenUtils;
import com.eternal.framework.utils.Utils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Completable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers;

public class NotificationDialog {
    private View root;
    private TextView info;
    private MaterialDialog dialog;
    private int height = ConvertUtils.dp2px(95);
    private int widthDiver = ConvertUtils.dp2px(20);
    private int duration = 500;
    private int sense;
    private Disposable disposable;
    private Intent intent;

    public NotificationDialog(Context context) {
        initView(context);
        dialog = new MaterialDialog.Builder(context)
                .backgroundColor(0x00ffffff).autoDismiss(false).canceledOnTouchOutside(false)
                .customView(root, false)
                .build();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtils.getAppScreenWidth() - widthDiver;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.dimAmount = 0;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        window.setAttributes(params);
        window.setGravity(Gravity.TOP);
        ViewGroup.LayoutParams p = root.getLayoutParams();
        p.height = height;
        root.setLayoutParams(p);
    }

    private void initView(Context context) {
        root = LayoutInflater.from(context).inflate(R.layout.pop_notification, null);
        info = root.findViewById(R.id.notify_info);
        root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (intent != null) {
                    disposable.dispose();
                    Utils.getContext().startActivity(intent);
                }
                dismiss();
            }
        });
    }

    public NotificationDialog setDismissListener(MaterialDialog.OnDismissListener listener) {
        dialog.setOnDismissListener(listener);
        return this;
    }

    public NotificationDialog setSense(int miles) {
        sense = miles;
        return this;
    }

    public NotificationDialog setIntent(Intent intent) {
        this.intent = intent;
        return this;
    }

    public boolean isShowing() {
        return dialog.isShowing();
    }

    public NotificationDialog setMessage(String msg) {
        info.setText(msg);
        return this;
    }

    public void show(String msg) {
        info.setText(msg);
        dialog.show();
        Animation animation = new TranslateAnimation(0, 0, -height, 0);
        animation.setDuration(duration);
        root.startAnimation(animation);
        disposable = Completable.timer(sense, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).subscribe(new Action() {
            @Override
            public void run() {
                dismiss();
            }
        });
    }

    public void show() {
        dialog.show();
        Animation animation = new TranslateAnimation(0, 0, -height, 0);
        animation.setDuration(duration);
        root.startAnimation(animation);
        disposable = Completable.timer(sense, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).subscribe(new Action() {
            @Override
            public void run() {
                dismiss();
            }
        });
    }

    public void dismiss() {
        Animation animation = new TranslateAnimation(0, 0, 0, -height);
        animation.setDuration(duration);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dialog.dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        root.startAnimation(animation);
    }
}
